<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all"/>
</head>
<body>
<div class="layui-container" style="width:600px;">
    <form class="layui-form layui-form-pane" action="" lay-filter="info">
        <input name="id" hidden>
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input th:if="${flagType} eq 'edit'" type="text" name="username" lay-verify="title" disabled="disabled"
                       style="background:#eeeeee!important" autocomplete="off" placeholder="请输入账号"
                       class="layui-input layui-disabled ">
                <input th:if="${flagType} eq 'add'" type="text" name="username" lay-verify="title" autocomplete="off"
                       placeholder="请输入账号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">真实姓名</label>
            <div class="layui-input-block">
                <input type="realName" name="realName" placeholder="请输入真实姓名" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="tel" name="phone" placeholder="请输入手机号" lay-verify="required|phone" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" lay-verify="email" placeholder="请输入邮箱" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input th:if="${flagType} eq 'add'" type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁止"
                       lay-filter="switch" checked="checked">
                <input th:if="${flagType} eq 'edit'" type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁止"
                       lay-filter="switch" checked="checked" disabled="disabled">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">单选框</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="1" title="男">
                <input type="radio" name="sex" value="2" title="女">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
            </div>
        </div>
    </form>
</div>
</body>
<script th:inline="javascript" type="text/javascript">
    var ctx = '[[@{/}]]'.replaceAll("\"", "").replace('//', '/');
</script>
<script th:src="@{/layui/layui.all.js}"></script>
<script th:src="@{/js/core.util.js}"></script>
<script>
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var form = layui.form;
    var element = layui.element;
    $(function () {
        CoreUtil.sendGet(ctx + "sys/user", null, function (res) {//查询用户详情填充表单
            //console.log("res="+ JSON.stringify(res));
            if (res.data != null) {
                $(".layui-container input[name=id]").val(res.data.id);
                $(".layui-container input[name=username]").val(res.data.username);
                $(".layui-container input[name=realName]").val(res.data.realName);
                $(".layui-container input[name=email]").val(res.data.email);
                $(".layui-container input[name=phone]").val(res.data.phone);
                $(".layui-container input[name=sex][value=1]").attr("checked", res.data.sex == 1 ? true : false);
                $(".layui-container input[name=sex][value=2]").attr("checked", res.data.sex == 2 ? true : false);
                if (data.status == 1) {
                    form.val('info', {
                        "status": true //开关状态
                    });
                    $(".layui-container input[name=status]").val(1);
                } else {
                    form.val('info', {
                        "status": false //开关状态
                    });
                    $(".layui-container input[name=status]").val(0);
                }
                form.render(); //更新全部
            }
        });

        //监听提交
        form.on('submit(submit)', function (data) {
            var data=data.field;
                if(data.status=="on"){
                    data.status=1;
                }else {
                    data.status=0;
                }
            //console.log("data="+ JSON.stringify(data));
            CoreUtil.sendPut(ctx + "sys/user/info", data, function (res) {
                layer.msg(res.msg);
            });
            return false;
        });

        //监听指定开关
        form.on('switch(switch)', function () {
            $(".layui-container input[name=status]").attr('type', 'hidden').val(this.checked ? 1 : 0);

        });
    })
</script>
</html>